package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.xerces.internal.impl.xpath.XPath;
import org.example.domain.Employee;
import org.example.domain.R;
import org.example.service.EmployeeService;
import org.example.service.impl.EmployeeServiceimpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService serviceimpl;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        String password = employee.getPassword();
        String s = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> employeeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        employeeLambdaQueryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee one = serviceimpl.getOne(employeeLambdaQueryWrapper);
        if (one == null) {
            return R.error("error");
        }
        if (!one.getPassword().equals(s)) {
            return R.error("error");
        }
        if (one.getStatus() == 0) {
            return R.error("error");
        }
        request.getSession().setAttribute("employee", one.getId());
        return R.success(one);


    }

    @PostMapping("/logout")
    public R<String> loginOut(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute("employee");
        session.invalidate();

//        R<String> success = R.success("success");
        return R.success("success");
    }

    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        Object employee1 = request.getSession().getAttribute("employee");
//        employee.setCreateUser((Long)employee1);
//        employee.setUpdateUser((Long)employee1);
        serviceimpl.save(employee);
        return R.success("success");

    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        Page<Employee> employeePage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> employeeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        employeeLambdaQueryWrapper.like(name != null, Employee::getName, name);
        serviceimpl.page(employeePage, employeeLambdaQueryWrapper);
        return R.success(employeePage);
    }

    @PutMapping
    public R<String> updata(HttpServletRequest request, @RequestBody Employee employee) {
        Object employee1 = request.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser((Long) employee1);
        boolean b = serviceimpl.updateById(employee);
        return R.success("success");
    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id) {
        Employee employee = serviceimpl.getById(id);
        if (employee == null) {
            return R.error("error");
        }
        return R.success(employee);
    }
}
